/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: ChatServerWithClientIdentification.java
 * Author: ckj
 * Date: 2024/7/21 下午10:14
 * Description:
 * 用于对应的Message标识
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.server;

import com.bosssoft.factory.CustomThreadFactory;
import com.bosssoft.factory.ObserverFactory;
import com.bosssoft.handle.ClientHandler;
import com.bosssoft.message.BaseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ckj
 */
public class ChatServerWithClientIdentification  {

    private static final int PORT = 8888;
    private ServerSocket serverSocket;
    private ThreadPoolExecutor executor;

    private static volatile Map<String, UserData> clientMap = new ConcurrentHashMap<>();
    private static volatile Map<String, List<BaseMessage>> messageMap = new HashMap<>();


    private static volatile List<UserObserver> userObservers = new ArrayList<>();

    private static final Logger logger = LoggerFactory.getLogger(ChatServerWithClientIdentification.class);

    /**
     *
     * @author ckj
     * @date 2024/07/22 上午10:33
     * 初始化操作
     */
    public ChatServerWithClientIdentification() {
        try {
            setServerSocket(new ServerSocket(PORT));
            setExecutor(new ThreadPoolExecutor(
                    5, 10, 60, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(20), new CustomThreadFactory("发送消息线程")
            ));
            loadObserver();
        } catch (IOException e) {
            getLogger().error("初始化线程失败");
        }
    }

    public static void main(String[] args) {
        ChatServerWithClientIdentification server = new ChatServerWithClientIdentification();
        server.start();
    }

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午4:18
     * 加载服务端
     */
    public void loadObserver(){
        getUserObservers().addAll(ObserverFactory.getAllUserObserver());
    }
    /**
     *
     * @author ckj
     * @date 2024/07/22 下午4:18
     * 启动服务端
     */
    public void start() {
        while (true) {
            try {
                Socket socket = getServerSocket().accept();
                getExecutor().execute(new ClientHandler(socket));
                getLogger().info("开始执行");
            } catch (IOException e) {
                getLogger().error("失败");
            }
        }
    }


    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }

    public void setExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }


    public static int getPORT() {
        return PORT;
    }

    public static Map<String, UserData> getClientMap() {
        return clientMap;
    }

    public static void setClientMap(Map<String, UserData> clientMap) {
        ChatServerWithClientIdentification.clientMap = clientMap;
    }

    public static Map<String, List<BaseMessage>> getMessageMap() {
        return messageMap;
    }

    public static void setMessageMap(Map<String, List<BaseMessage>> messageMap) {
        ChatServerWithClientIdentification.messageMap = messageMap;
    }

    public static List<UserObserver> getUserObservers() {
        return userObservers;
    }

    public static void setUserObservers(List<UserObserver> userObservers) {
        ChatServerWithClientIdentification.userObservers = userObservers;
    }

    public static Logger getLogger() {
        return logger;
    }
}
